package cn.wsalix.sale.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.anlaser.service.impl.CommonServiceImpl;
import cn.anlaser.utils.SearchFilter;
import cn.anlaser.utils.SearchFilter.Operator;
import cn.wsalix.order.entity.Order;
import cn.wsalix.sale.entity.SaleDaily;
import cn.wsalix.sale.form.SaleDailyForm;
import cn.wsalix.sale.repository.SaleDailyRepository;
import cn.wsalix.sale.service.SaleDailyService;
import cn.wsalix.sys.service.DailyRecordService;

/**
 * @author jskyme AOP销售记录,在购买付款后调用计数方法
 */
@Aspect
@Service("saleDailyService")
public class SaleDailyServiceImpl extends
		CommonServiceImpl<SaleDailyForm, SaleDailyRepository, SaleDaily>
		implements SaleDailyService {
	@Autowired
	private SaleDailyRepository saleDailyRepository;

	@Pointcut("execution(* cn.wsalix.order.service.impl.OrderServiceImpl.aop(..))")
	public void orderWxPay() {
	};

	@Autowired
	private DailyRecordService dailyRecordService;// 记录订单数/订单付款数
	Order order = null;
	@Before("orderWxPay()")
	public void before(JoinPoint call) {	
		logger.info("【注解-前置通知】:方法正常结束了");
		Object[] args = call.getArgs();
		if (args.length == 1) {
			if (args[0] instanceof Order) {
				order = (Order) args[0];
			}
		}
	}
	@Transactional
	@Around("orderWxPay()")
	public Object doBasicProfiling( ProceedingJoinPoint pjp)
			throws Throwable {
		// start stopwatch	
		Object retVal = pjp.proceed();
		if (retVal instanceof Boolean) {
			Boolean val = (Boolean) retVal;
			if (val) {
				if (order != null) {
					saleCountIncrement(order);
				}
			}
		}
		// stop stopwatch
		// 这里，这里，这里添加代码，你就把retVal想干嘛就干嘛，然后再返回
		return retVal;
	}

	@AfterReturning("orderWxPay()")
	public void afterReturn(JoinPoint call) {
		logger.info("【注解-后置通知】:方法正常结束了");
	}

	@Override
	public String module() {
		return "/sale/sale_daily";
	}

	@Override
	public SaleDailyRepository getRepository() {
		return saleDailyRepository;
	}

	@Override
	public Class<SaleDaily> getEntityClazz() {
		return SaleDaily.class;
	}

	@Override
	public Map<String, SearchFilter> buildByForm(SaleDailyForm form) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		if (form.getDaily() != null && !form.getDaily().equals("")) {
			filters.put("daily",
					new SearchFilter("daily", Operator.EQ, form.getDaily()));
		}
		if (form.getProductId() != null && form.getProductId() != 0) {
			filters.put("productId", new SearchFilter("productId", Operator.EQ,
					form.getProductId()));
		}
		return filters;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.wsalix.sale.service.SaleDailyService#newDay() 该方法不使用
	 */
	@Transactional
	@Override
	public void newDay() {
		SaleDaily saleDaily = new SaleDaily();
		saleDaily.setDaily(new Date());
		// super.add(saleDaily);
	}
	@Override
	public SaleDaily findByCurrDayAndProduct(Long productId) {
		return saleDailyRepository.findByCurrDayAndProduct(productId);
	}
	@Transactional
	@Override
	public void saleCountIncrement(Order order) {
		SaleDaily saleDaily = findByCurrDayAndProduct(order.getProduct().getId());
		if(saleDaily==null){
			saleDaily=new SaleDaily();
			saleDaily.setDaily(new Date());
			saleDaily.setProductId(order.getProduct().getId());
			saleDaily.setSaleCount(order.getNum());
			saleDaily=add(saleDaily);
		}else{
			//newByCurrDayAndProduct
			saleDaily.setSaleCount(saleDaily.getSaleCount()+order.getNum());
			//saleDailyRepository.saleCountIncrement(order.getProduct().getId(),order.getNum());
			edit(saleDaily);
		}
	}

}
